<?php

class CaseMerchandise extends Zend_Db_Table_Abstract
{
	protected $_name = 'case_merchandise';
	
	private $_errorCode = '';
	private $_errorStr = '';
	
	public function getErrorCode() { return $this->_errorCode; }
	public function getErrorStr() { return $this->_errorStr; }
	
	public function getSelectionsForCase( $FDMS_case_num )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}
		
		$items = array();
		
		try
		{
			$rows = $this->fetchAll("FDMS_case_num='$FDMS_case_num' and item_category in ('Casket','Urn','Vault','Urn Vault')");
			foreach( $rows as $row )
				$items[] = $row->toArray();
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $items;
	}
	
	public function setSelectionsForCase( $FDMS_case_num )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}

		// first, delete all existing items for tihs case
		if($replace) $this->delete("FDMS_case_num='$FDMS_case_num'");
		
		// then insert the new ones
		try
		{
			$rowsInserted = 0;
			foreach( $items as $item )
			{
				$item['FDMS_case_num'] = $FDMS_case_num;
				$rowsInserted += $this->insert( $item );
			}
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $rowsInserted;
	}
	
	public function getItemsForCase( $FDMS_case_num )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}
		
		$items = array();
		
		try
		{
			$rows = $this->fetchAll("FDMS_case_num='$FDMS_case_num'");
			foreach( $rows as $row )
				$items[] = $row->toArray();
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $items;
	}
	
	public function setCasketAndVaultForCase( $casketItem, $vaultItem, $FDMS_case_num )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}

		// first, delete all existing items for tihs case
		$this->delete("FDMS_case_num='$FDMS_case_num' and item_category='Casket'");
		$this->delete("FDMS_case_num='$FDMS_case_num' and item_category='Casket Vault'");
		
		// then insert the new ones
		try
		{
			$items = array( $casketItem, $vaultItem );
			$rowsInserted = 0;
			foreach( $items as $item )
			{
				$item['FDMS_case_num'] = $FDMS_case_num;
				$rowsInserted += $this->insert( $item );
			}
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $rowsInserted;
	}
	
	public function setUrnAndVaultForCase( $urnItem, $vaultItem, $FDMS_case_num )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}

		// first, delete all existing items for tihs case
		$this->delete("FDMS_case_num='$FDMS_case_num' and item_category='Urn'");
		$this->delete("FDMS_case_num='$FDMS_case_num' and item_category='Urn Vault'");
		
		// then insert the new ones
		try
		{
			$items = array( $urnItem, $vaultItem );
			$rowsInserted = 0;
			foreach( $items as $item )
			{
				$item['FDMS_case_num'] = $FDMS_case_num;
				$rowsInserted += $this->insert( $item );
			}
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $rowsInserted;
	}

	public function setItemsForCase( &$items, $FDMS_case_num, $replace=true )
	{
		if(strlen($FDMS_case_num)<1)
		{
			$this->_errorCode = 'INVALID_PARAMETER';
			$this->_errorStr = 'Missing or empty case number';
			return null;	
		}

		// first, delete all existing items for tihs case
		if($replace) $this->delete("FDMS_case_num='$FDMS_case_num'");
		
		// then insert the new ones
		try
		{
			$rowsInserted = 0;
			foreach( $items as $item )
			{
				$item['FDMS_case_num'] = $FDMS_case_num;
				$rowsInserted += $this->insert( $item );
			}
		}
		catch(Exception $e)
		{
			$this->_errorCode = 'SQL_ERROR';
			$this->_errorStr = ''.$e;
			$items = null;	
		}
		
		return $rowsInserted;
	}
}

?>